package jdbc;

import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class UpdateStudentName {
    public static void main(String[] args) {
        //输入相关值
        Scanner in = new Scanner(System.in);
        System.out.print("请输入学号：");
        long stuno=in.nextLong();
        in.nextLine();//去掉那个回车
        System.out.print("请输入name：");
        String name=in.nextLine();

        System.out.print("请输入qq号：");
        String qq=in.nextLine();
        Connection con = DBUtil.get();
        PreparedStatement ps=null;
        String sql="update s_student set sname=?,qq=? where stuno=?";
        try {
            //预编译上下文
             ps = con.prepareStatement(sql);
             //填充？的值
             ps.setString(1,name);
             ps.setString(2,qq);
             ps.setLong(3,stuno);
             //更新
            int row = ps.executeUpdate();//sql作为参数
            System.out.println("更新了"+row+"行");

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(null,ps,con);
        }
    }
}
